﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using System.Data.SqlClient;
using DAL.Entities;

namespace DAL.Entities_DAL
{
    public class Khachhang_DAL: BaseDAL,UTL.IBaseDAL
    {
        public int Count()
        {
            throw new NotImplementedException();
        }

        public DataTable Select()
        {
            var sql = @"SELECT * FROM KHACH_HANG";
            return ExecuteQuery(sql);
        }

        public DataTable Select(object obj)
        {
            throw new NotImplementedException();
        }

        public object GetByKey(object key)
        {
            throw new NotImplementedException();
        }

        public bool Delete(string id)
        {
            string sql = @"DELETE FROM KHACH_HANG WHERE MA_KH = '{0}'";
            sql = string.Format(sql, id);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public bool Insert(object obj)
        {
            var o = (Khachhang)obj;
            string sql = @"INSERT INTO KHACH_HANG(MA_KH, HOTEN_KH, D_CHI, SDT, NG_SINH) VALUES ( '{0}', N'{1}', N'{2}', '{3}', '{4}' ) ";
            sql = string.Format(sql, o.MA_KH, o.HOTEN_KH, o.D_CHI, o.SDT, o.NG_SINH);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public bool Update(object obj)
        {
            var o = (Khachhang)obj;
            string sql = @"set dateformat dmy; UPDATE KHACH_HANG SET HOTEN_KH= N'{1}', D_CHI = N'{2}', SDT = '{3}', NG_SINH = '{4}' WHERE MA_KH='{0}'";
            sql = string.Format(sql, o.MA_KH, o.HOTEN_KH, o.D_CHI, o.SDT, o.NG_SINH);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public DataTable Search(string name)
        {
            throw new NotImplementedException();
        }

        //tim ma so lon nhat

        public DataTable Select_max()
        {
            var sql = @"SELECT MAX(MA_KH) as id FROM KHACH_HANG";
            return ExecuteQuery(sql);
        }

        //dem so khach hang

        public string Select_count()
        {
            string sql = @"SELECT count(MA_KH) as id FROM KHACH_HANG";
            var tb = ExecuteQuery(sql);
            return tb.Rows.Count > 0 ? tb.Rows[0]["id"] + "" : "0";
        }

        // load khach hang len combobox

        public DataTable Select_kh()
        {
            var sql = @" select ma_kh as id, hoten_kh as ten from khach_hang";
            return ExecuteQuery(sql);
        }
    }
}
